import { defineStore } from "pinia";
import { configList } from "./indicatorConfig";

interface IState {
    list: IItem[];
    popIndicator: boolean;
}

interface IItem {
    title: string;
    value: string;
    main: boolean;
    selected?: boolean;
    id: string;
}

export const useIndicator = defineStore("indicator", {
    state: (): IState => ({
        list: configList.map(i => {
            // return { ...i, selected: false, id: "" };
            return { ...i, id: "" };

        }),
        popIndicator: false
    }),
    getters: {
        main(state) {
            let ary = state.list.filter(i => i.main);
            // console.log("主图", ary)
            return ary;
        },
        sub(state) {
            return state.list.filter(i => !i.main);
        }
    },
    actions: {
        toggleItem(item: IItem) {
            item.selected = !item.selected;
        },
        clearSelected() {
            this.list.forEach(i => (i.selected = false));
        },
        getByValue(name: string) {
            return this.list.find(i => name == i.value);
        },
        getSubByValue(name: string) {
            return this.sub.find(i => name == i.value);
        },
        getSubOpen() {
            return this.sub.filter(i =>i.selected);
        },
        getMainOpen() {
            return this.main.filter(i =>i.selected);
        },
        changePopIndicator(val: boolean) {
            this.popIndicator = val;
        },
        togglePopIndicato() {
            this.popIndicator = !this.popIndicator;
        }
    }
});
